package com.ebelter.myehc.ui.fragments.modifys;

import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.BaseResponse;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.WheelView;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.ebelter.btcomlib.views.dialogs.WheelViewDialog;
import com.ebelter.myehc.R;
import com.ebelter.myehc.models.http.request.EhcNetUtils;
import com.ebelter.myehc.ui.activitys.MainActivity;
import com.ebelter.myehc.ui.view.CombinView6;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import java.util.List;

import static com.ebelter.btcomlib.views.dialogs.TextTipDialog.Gravity_BUTTOM;



/**
 * 血糖
 */

public class ModifyHeight_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "ModifyHeight_Fragment";

    private TextView m_title_tv;
    private TextView m_righttitle_tv;
    private ImageView top_left_iv;
    
    private CombinView6 height_cb6;

    private int height = 100;


    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_modify_height;
    }

    @Override
    protected void initView() {
        super.initView();

        FV();
        m_title_tv.setText(R.string.str_Stature);
        m_righttitle_tv.setText(R.string.done);

       int height2= UserSpUtil.readInt(Constants.IUser.HEIGHT);
       if (height2>0){
           height=height2;
       }

        height_cb6.setTitle2(height2+" CM");
    }
    @Override
    protected void setListener() {
        super.setListener();

        m_righttitle_tv.setOnClickListener(this);
        top_left_iv.setOnClickListener(this);
        height_cb6.setOnClickListener(this);

    }




    private void FV() {
        m_title_tv=mRootView.findViewById(R.id.m_title_tv);
        m_righttitle_tv=mRootView.findViewById(R.id.m_righttitle_tv);
        top_left_iv=mRootView.findViewById(R.id.top_left_iv);
        height_cb6=mRootView.findViewById(R.id.height_cb6);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.m_righttitle_tv:
                complete ();
                break;
            case R.id.top_left_iv:
                back();
                break;
            case R.id.height_cb6:
                height_cb6_Click();
                break;
        }
    }

    private void height_cb6_Click() {

        if (getContext()==null) return;

        height_cb6.setImage1(R.drawable.ic_shangla);


        WheelViewDialog wheelViewDialog = new WheelViewDialog(getContext());
        wheelViewDialog.setGravity(Gravity_BUTTOM);


        List<String> list=new ArrayList<>();
        for (int i=100;i<230;i++){
            list.add(i+" CM");
        }
        wheelViewDialog.setOffeset(1);
        wheelViewDialog.setItems(list);
        wheelViewDialog.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                Log.i(TAG, "selectedIndex: " + selectedIndex + ", item: " + item);
                height = NumUtils.string2Int(item.replace(" CM",""));
            }
        });


        wheelViewDialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }

            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();

                if (height_cb6!=null)height_cb6.setTitle2(height+" CM");
            }
        });

        wheelViewDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (height_cb6!=null) height_cb6.setImage1(R.drawable.ic_xiala);
            }
        });
        wheelViewDialog.setSelectPosition(height-100);
        wheelViewDialog.show();





    }

    //更新身高信息
    private void updatePersonHeight(final int height) {

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }




        String authKey = MainActivity.getAuthKey();
        EhcNetUtils.getInstance().updatePersonHeight(this, authKey, height, new HttpResponse<BaseResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, BaseResponse baseResponse, String s) {
                LogUtils.i(TAG,s);
                if (isRequestSuccess&&baseResponse.resultCode==1){
                    //说明修改成功
                    ToastUtil.show(R.string.Modify_Success);

                    UserSpUtil.writeInt(Constants.IUser.HEIGHT,height);
                    ScaleUser.getUser().setHeight(height);

                    if (height_cb6!=null)height_cb6.setTitle2(height+" CM");

                    // TODO: 2018/8/28   如果此时连接的是体脂秤 需要下发新的用户信息给秤
                    CommonEventBus commonEventBus = new CommonEventBus(TAG, MainActivity.TAG,0,"设置了身高-需要重新下发用户信息");
                    EventBus.getDefault().post(commonEventBus);

                }else {
                    //说明修改失败
                    ToastUtil.show(R.string.Modify_Failed);
                }



            }
        });

    }

    private void back() {   if (mActivity!=null)mActivity.finish();
    }

    private void complete() {
        updatePersonHeight(height);
    }
}
